#ifndef PopoonLocalPath_H
#define PopoonLocalPath_H
#include <stdio.h>
#include <iostream>
#include <QtCore>
#include <QDebug>
#include <QPointer>
#include <QSqlError>
#include <QSqlQuery>
#include <QSqlTableModel>
#include <QSqlQueryModel>
#include <QSqlField>
#include <QSqlIndex>
#include <QXmlStreamWriter>
#include <QXmlQuery>
#include "QmyDom.h"

class StreamBuf
{
  public:
  StreamBuf() 
  :d(new QBuffer())
  {
  d->open(QIODevice::ReadWrite);
  }
  ~StreamBuf() { d->close(); }
  bool clear() {
    d->write(QByteArray()); 
    return d->bytesAvailable() == 0 ? true : false;
  }
  bool writefull( QByteArray chunk) {
    clear();
    d->write(chunk);
    start();
    return d->bytesAvailable() == 0 ? false : true;
  }
  void start() { 
    d->seek(0);
  }
  
  bool savechunk( const QString file , QByteArray chunk )
  {
      clear();
      start();
      device()->write(chunk); 
       start();
     return PutOnFile(file);
  }

  bool PutOnFile( const QString file ) { 
      QFile f(file);
      start();
      if (f.open(QFile::WriteOnly)) {
        uint bi = f.write(d->readAll());
        f.close();
        start();
        return bi > 0 ? true : false;
      }
   return false; 
  }
  bool LoadFile( const QString file ) { 
    if (clear()) {
     QFile f(file); 
        if (f.exists()) {
          if (f.open(QFile::ReadOnly)) {
           d->write(f.readAll());
           f.close();
           start();
           return true;
          }
        }
    }
    return false;
  }
  QIODevice *device() { return d; }
  QByteArray stream() { return d->data(); }
  /* <?xml version="1.0" encoding="utf-8"?> */
  QString data() { return QString::fromUtf8(stream()); }
  QBuffer *d;
};




class Sitemap : public QThread
{
public:
    Sitemap( QObject *parent = 0 )
            : QThread(parent)
    {
        setTerminationEnabled ( true );
    }
protected:
    void run() {
        exec();
    }
};





class PopoonLocalPath : public QObject
{
     Q_OBJECT

public:
    PopoonLocalPath( const  QString mapfile );

protected:
      void loadComponents( const QDomElement e );
      void loadPipe( const QDomElement e );

      bool testrunPipe( const QDomElement e  );
      void createGenerators( QDomNode source , QDomElement append  );
      void importOnCurrentNode( const QByteArray xml , QDomElement append );

      void appendParams(  QDomNode params , QXmlQuery * xsltquery  );
      void setPart( const QDomElement e );
      QVariant param_eval( const QString value );

     bool setConfig( const QString dir  );
     bool cleaned_end;
     QFileInfo fi;
     QFileInfo ficonf;
     bool started;
     int pipeloop;
     bool generator_strip_root;

private:
     QByteArray Cxml;
     bool xmlinstream;
    
signals:
    void terminate();
    void error_msg(QString,bool);
    void handle_msg(QString);
public slots:
    void processing();
    void onclose();
private slots:
    void read();

};
//
#endif // PopoonLocalPath_H

